package com.fenqing.comics.utils;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

/**
 * @author fenqing
 */
public class CollUtils {

    public static <S, T> List<T> map(Collection<S> collection, Function<S, T> mapper){
        return collection.stream().map(mapper).toList();
    }

    public static <T> boolean listContentSame(List<T> list1, List<T> list2){
        if (list1.size() != list2.size()) {
            return false;
        }
        Map<T, Integer> count1 = MapUtils.count(list1);
        for (T t2 : list2) {
            Integer count = count1.get(t2);
            if (count == null || count <= 0) {
                return false;
            }
            count1.put(t2, count - 1);
        }
        Map<T, Integer> count2 = MapUtils.count(list2);
        for (T t1 : list1) {
            Integer count = count2.get(t1);
            if (count == null || count <= 0) {
                return false;
            }
        }
        return true;
    }

}
